
export const initState={
    // 初始化user
    user: {
        phone: "1",
        password: "1"
    },
    info:sessionStorage.getItem("user")?JSON.parse(sessionStorage.getItem("user")):{},
}

export const reducer=(state,action)=>{
    switch(action.type){
        case "changeUser":
            return {
                ...state,
                user:{
                    ...state.user,
                    [action.key]:action.value
                }
            }
        case "changeInfo":
            return {
                ...state,
                info:action.info
            }
        default:
            return state;
    }
}

export const actions={
    // 修改user的action
    changeUser:(key,value)=>({type:"changeUser",key,value}),
    
    // 绑定仓库和本地
    changeInfo:(info)=>{
        if(info.token){
            sessionStorage.setItem("user",JSON.stringify(info))
        }else{
            sessionStorage.removeItem("user")
        }
        return {type:"changeInfo",info}
    },
}